<template>
  <div class="item product css-slide--snap card">
    <div class="favorite-badge active" @click="collectedGoods(goodsInfo.goods_id)" v-if="$route.name === 'pcMyCollect'">
      <i class="fa-solid fa-trash"></i>
    </div>

    <div @click="newWindowGoods">
      <div class="img-wrapper">
        <q-img :src="goodsInfo.main_picture" no-native-menu fit="cover">
        </q-img>
      </div>


      <div class="detail spacing--xlarge product-item__text card__text">
        <div class="price">
          <div class="price-desc">
            <span>${{ formattedPrice(goodsInfo.shop_min_price) }} - ${{formattedPrice(goodsInfo.shop_max_price)}}</span>
          </div>
        </div>

        <a href="javascript:;" class="goodName"><div class="title-inner"><span>{{goodsInfo.name}}</span></div></a>

      </div>

      <div class="buy-btn">
        <div></div>
        <q-btn no-caps outline rounded color="black" label="Buy now" size="17px" padding="12px" style="border-radius: 30px"/>
      </div>
    </div>
  </div>
</template>

<script setup lang='ts'>
import {defineProps, defineEmits, defineExpose, ref, computed} from 'vue'
import {useRouter} from "vue-router";
import {goodsCollected} from "../../../api/goods";
import {Notify} from "quasar";
const emit = defineEmits(["deleteG"])

const router = useRouter()
const ratingModel = ref(0)

const props = defineProps({
  goodsInfo: {
    type: Object,
    require: true,
    default: {}
  }
})
// console.log(props.goodsInfo)
// console.log(123)
ratingModel.value = parseFloat(props.goodsInfo.score)

const formattedPrice = computed(() => {
  return (price) => (price / 100).toFixed(2);
});

const newWindowGoods = () => {
  router.push(`/pc/goods/${props.goodsInfo.goods_id}`)
  // window.open(newpage.href, '_blank')
}

const collectedGoods = (goods_id) => {
  goodsCollected({goods_id: goods_id}).then((res) => {
    Notify.create({
      color: 'green',
      textColor: 'white',
      icon: 'check',
      message: 'collected',
      position: 'top-right',
    })
    emit("deleteG", goods_id)
  })
}

defineExpose({})
</script>

<style scoped lang='less'>
.item {
  background-color: white;
  border-radius: 10px;
  flex: 0 0 calc(50% - 2px); /* 每个元素占据25%的宽度 */
  border: 1px solid #ccc; /* 边框 */
  margin-right: 14px; /* 间隙 */
  //text-align: center;
  //&:nth-child(2){
  //  margin-right: unset;
  //}
  overflow: hidden; /* 防止放大的图片溢出容器 */
  .img-wrapper{
    aspect-ratio: 8/9;
    overflow: hidden;
    .item{

    }
  }
  .detail{
    padding: 25px 25px 5px 25px;
    .price{
      font-size: 20px;
      margin-bottom: 5px;
      .price-desc{
        color: var(--q-secondary)
      }
    }
    .goodName{
      font-size: 1.125rem;
      font-weight: 800;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;  /* 强制最多显示2行 */
      overflow: hidden;
      text-overflow: ellipsis;
      line-height: 1.5;       /* 必须设置行高 */
      height: 3em;            /* 关键：行高1.5 * 2行 = 3em */
      min-height: 3em;        /* 确保最小高度 */
      box-sizing: border-box; /* 防止padding影响高度 */
      display: -moz-box;
      display: -ms-flexbox;
      max-height: 3em;        /* 双重保障 */
    }
    .title{
      font-size: 18px;
      font-weight: 800;
      .title-inner{
        margin-bottom: -10px;
      }
      span{
        position: relative;
        &::after {
          content: '';
          position: absolute;
          left: 0;
          bottom: 0;
          width: 0;
          height: 1px;
          background-color: #000000; /* 边框颜色 */
          transition: width 0.5s ease; /* 过渡效果 */
        }
        &:hover::after {
          width: 100%; /* 鼠标悬浮时边框宽度为100% */
        }
      }
    }
    .tag-wrapper{
      display: flex; /* 启用 flexbox 布局 */
      flex-wrap: wrap; /* 当容器内没有足够空间时，允许子元素换行 */
      gap: 3px;
      .tag{
        font-size: 14px;
        padding: 5px;
        background-color: #c5c5c5;
        border-radius: 4px;
      }
    }
    .product-item__ratings{
      .remove-empty-space>*:last-child {
        margin-bottom: 0;
      }
      .star-rating {
        font-size: 16px;
        width: 100%;
        height: 25px;
        display: grid;
        grid-template-columns: max-content auto;
        align-items: center;
        gap: 0.625rem;
        .palm-hide{
          white-space: nowrap;
        }
      }
      .visually-hidden {
        clip: rect(0 0 0 0);
        clip-path: inset(50%);
        height: 1px;
        overflow: hidden;
        position: absolute;
        white-space: nowrap;
        width: 1px;
      }
      .card .star-rating__stars {
      }
      .star-rating__stars {
        background-image: url(./pentagram.svg);
        background-repeat: repeat-x;
        font-size: 0;
        display: inline-block;
        text-align: left;
      }
      .card .star-rating__stars-active {
      }
      .star-rating__stars-active {
        background-image: url(./pentagram-fill.svg);
        display: inline-block;
        font-size: 0;
        height: 16px;
        background-repeat: repeat-x;
      }
    }
  }
  .buy-btn{
    padding: 0 25px 25px 25px;
    display: flex;
    flex-direction: column;
    justify-content: stretch;
  }
  .spacing--xlarge > * {
    margin-bottom: 14px;
  }
  .product-item__text {
    display: flex;
    flex-direction: column;
    justify-content: stretch;
  }
  .card__text{
    z-index: 99;
    word-wrap: break-word;
    word-break: break-word;
  }
  .text-align--left{
    text-align: start;
  }
}
.item{
  cursor: pointer;
}
.item:last-child {
  margin-right: 0; /* 最后一个元素不需要间隙 */

}


.q-img:hover{
  transform: scale(1.05); /* 当鼠标悬浮时放大图片 5% */
  transition: transform 0.3s ease-in-out; /* 添加平滑的过渡效果 */
}

.product{
  display: flex;
  flex-direction: column;
  overflow: visible;
}
.css-slide--snap{
  scroll-snap-align: start;
}
.card{
  --border-width-cards: 1px;
  --border-radius-cards: 10px;
  --border-width-buttons: 1px;
  --border-radius-buttons: 30px;
  --border-width-forms: 1px;
  --border-radius-forms: 5px;
  --shadow-x-cards: 0px;
  --shadow-y-cards: 0px;
  --shadow-blur-cards: 0px;
  border: solid var(--border-width-cards) #e1e1e1;
  border-radius: var(--border-radius-cards);
  overflow: hidden;
  background-color: #ffffff;
  position: relative;
  color: #1d1d1d;
  box-shadow: var(--shadow-x-cards) var(--shadow-y-cards) var(--shadow-blur-cards) rgba(0,0,0,0);
  margin-bottom: calc(var(--shadow-y-cards) + var(--shadow-blur-cards));
  margin-inline-end: calc(var(--shadow-x-cards) + var(--shadow-blur-cards));
}
.favorite-badge {
  position: absolute;
  top: 0.9375rem;
  right: 0.9375rem;
  width: 2.25rem;
  height: 2.25rem;
  background: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0px 0rem 6px 5px rgba(0, 0, 0, 0.1);
  z-index: 5;
  transition: all 0.3s;
}

.favorite-badge:hover {
  background: #ffebee;
}

.favorite-badge.active {
  color: #e53935;
}
</style>